<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Link extends CI_Controller 
{
	protected $table = 'tbl_link';
	protected $width = 1000;
	protected $height = 800;
	protected $size = 800;
	protected $type ='gif|jpg|png';
	protected $patch = './upload/link/';
	protected $name_link = "link";
	function __construct()
	{
		parent::__construct();
		
		//load helper
		$this->load->helper('login_helper');
			$this->load->helper('html');
		
			//load model

		$this->load->model('Mlink');
		


    //load libraries

    $this->load->library('pagination');

    $this->load->library('form_validation');
	// session
	 session_start();
		 if ( !isset($_SESSION['row_number']))

			$_SESSION['row_number']= 12;
	
	
	// check login
	check_login();
	
	load_lang("admin");
	}
		////************************ News  **************************/////
	function index($page='')
	{
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/link/index/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mlink->count_all('tbl_banner');

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mlink->list_data($page,$limit);// $this->Muser->user_review($page, $limit);

    $data['title'] = 'hình slide';//$this->lang->line('title_link_review');

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'link_review';

    $this->load->view('admin/layout/template_main', $data);
	}
	
	// NEws create
	
	function create()
	{
		if ($this->input->post('create'))
    	{
			
			$this->form_validation->set_rules('ban_name', 'English Name', 'required');    
	   		
	//	$this->upload->initialize($config);
   
		if ($this->form_validation->run() == false )
		{	$data['missing'] = $this->form_validation->_error_array;}
    	  else
		{
		//	print_r();
		
			if($_FILES['img']['name'] != NULL )
				{
					if(!$this->upload_file())
					{	
						$data['missing']['img']= $this->upload->display_errors();
						$data['main'] = 'link_create';
						$data['num_lang'] = $this->num_lang;	
						$data['title'] = $this->lang->line('title_link_create');
						$this->load->view('admin/layout/template_main', $data);
						return false;		
					//	$this->load->view('admin/template_main', $data);
					}
				}
		//	return false;

			  if ($this->form_validation->run() && $this->link_create_sb())

        redirect('admin/link/', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			if(isset($_FILES['img']) && $_FILES['img']['error']==4)
				$data["missing"]=array("img"=>"Images field is required.");
			
		}

			
		}
      // $this->form_validation->set_rules('img', 'img', 'required|callback_upload_file');
	
    
    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/link/', 'refresh');

		}	
//	echo "<pre>";
	//print($data['cate']);
	//echo "</pre>";
		$data['main'] = 'link_create';
	//$data['cate'] = $this->Mlink->get_cate();
	
    $data['title'] = 'Thêm hình slide';//  $this->lang->line('title_link_create');

		$this->load->view('admin/layout/template_main', $data);
	}
		
			
				// function add link
	function link_create_sb()
	{

		if(isset($_POST["create"]))
		{
			$name = htmlentities($this->input->post("ban_name"),ENT_QUOTES,"UTF-8");
		$status = $this->input->post('status');
		$link = htmlentities($this->input->post("ban_website"),ENT_QUOTES,"UTF-8");
		$data = array(
			"ban_name" => $name,
			"ban_website" => $link,
			"ban_images" =>  $_POST['img'],//$_FILES['img']['name'] 
			"ban_modify"=>time(),
			"ban_status"=>$status
			);
			
	//
	
			
		if($this->Mlink->insert_data($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}	
	
	#Update link
		function update()
	{
		
	$id = $this->uri->segment(4);
		 if ($this->input->post('update'))
		
    {

      //set rules
	$this->form_validation->set_rules('ban_name', 'Tên tiêu đề', 'required');
  // $this->form_validation->set_rules('ban_website', 'Link liên kế', 'required');

         
      $config['upload_path'] = './upload/link/';
	  if(!is_dir($config['upload_path']) )
		{
			//echo 'as';
			@mkdir($path,777);
		}
							
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '800';
		$config['max_width'] = '800';
		$config['max_height'] = '800';
	
		$this->load->library('upload',$config);
	//	$this->upload->initialize($config);
   
		if ($this->form_validation->run() == false )
		{	$data['missing'] = $this->form_validation->_error_array;}
    	  else
		{
		//	print_r();
		
			if($_FILES['img']['name'] != NULL )
				{
					if(!$this->upload_file())
					{	
						$data['missing']['img']= $this->upload->display_errors();
							$data['result'] = $this->Mlink->info_from_id($id,'tbl_banner');
	
						//$data['cate'] = $this->Msub_cate->get_sub_cate();
						$data['main'] = 'link_update';
						//$data['num_lang'] = $this->num_lang;	
						$data['title'] = $this->lang->line('title_link_update');
				
						$this->load->view('admin/layout/template_main', $data);
						
						return false;		
					//	$this->load->view('admin/template_main', $data);
					}
					else
					{
						if( $_FILES['img']['error'] != 4 )
						{
							$file = $this->Mlink->info_from_id($id,'tbl_banner');
							$path = "upload/link/";
							$path = "upload/link/".$file[0]['ban_images'];
							@unlink($path);
						}
					}
				}
		//	return false;

  	  if ($this->form_validation->run() && $this->link_update_sb()  )
        redirect('admin/link/', 'refresh');
      else

        	$data['missing'] = $this->form_validation->_error_array;

		}
   }

    elseif ($this->input->post('close'))
		{
			redirect('admin/link/', 'refresh');
		}
		$data['main'] = 'link_update';
		$data["result"] =  $this->Mlink->info_from_id($id,'tbl_banner');
   		$data['title'] =  'Sửa hình slide';
		$this->load->view('admin/layout/template_main', $data);
	}
	
	# update proceessing
	function link_update_sb()
	{
		
		if(isset($_POST["update"]))
		{
			$id = $this->uri->segment(4);
			//else return false;
		
		
			$name = htmlentities($this->input->post("ban_name"),ENT_QUOTES,"UTF-8");
		$status = $this->input->post('status');
	$link = htmlentities($this->input->post("ban_website"),ENT_QUOTES,"UTF-8");
			
		if($_FILES['img']['error'] == 0)
		{						
			$data = array(
			"ban_name" => $name,
			"ban_website" => $link,
			"ban_images" =>  $_POST['img'],//$_FILES['img']['name'] 
				"ban_modify"=>time(),
			"ban_status"=>$status
			);
		}
		else
				$data = array(
			"ban_name" => $name,
			"ban_website" => $link,
				"ban_modify"=>time(),
			"ban_status"=>$status
			
			);
	//
	
			
		if($this->Mlink->update_data($data,$id))
			return true;
		else
			return false;
		}
		else
			return false;
	}
	function upload_file($file="",$input_file ='img')
 	{
	 	$_POST['img'] = $file;
    	if ($_FILES[$input_file]['error'] == 0  )
    	{
      		$path_directory = $this->patch;
      		if ( ! is_dir($path_directory))
				mkdir($path_directory);
      		$config['upload_path'] = $path_directory;
			$config['allowed_types'] = $this->type;
			$config['max_size'] = $this->size;
			$config['max_width'] = $this->width;
			$config['max_height'] = $this->height;
      		$this->load->library('upload',$config);
      		if ($this->upload->do_upload($input_file))
      		{
        		$info_file = $this->upload->data();
        		$file_path = $info_file['file_name'];
      			$_POST['img'] = $file_path;
      		}
	 		else
	  		{
		  		return false;
	  		}
    	}
		return true;
  	}

			
	
		////************************ News Category **************************/////
	
	
	
		// category  review	
	function cate()
	{
		$limit = $_SESSION['row_number'];

		$config['base_url'] = site_url().'/admin/link/cate/';

		$config['first_link'] = 'First';

		$config['last_link'] = 'Last';

		$config['next_link'] = '&gt;&gt;';

		$config['prev_link'] = '&lt;&lt;';

		$config['cur_tag_open'] = '<a href="#" class="current">';

		$config['cur_tag_close'] = '</a>';

		$config['uri_segment'] = 4;

		$config['num_links'] = 2;

		$config['total_rows'] = $this->Mlink->count_all('link_cate');

		$config['per_page'] = $limit;
		

		$this->pagination->initialize($config);

    if (empty($page))	$page = 0; //default value

    $data['result'] = $this->Mlink->list_cate($page,$limit);
	
	
	// $this->Muser->user_review($page, $limit);

    $data['title'] =  $this->lang->line('title_link_cate_review');

    $data['page'] = str_replace('&nbsp;', '', $this->pagination->create_links());

    $data['main'] = 'link_cate_review';

    $this->load->view('admin/template_main', $data);
	}
	
	
	// category create
	
	function cate_create()
	{
		 if ($this->input->post('create'))
		 

    {
	

      //set rules


      $this->form_validation->set_rules('title_en', 'Title English', 'required');

      $this->form_validation->set_rules('title_vi', 'Title Vietnamese', 'required');

  //   $this->form_validation->set_rules('title_cn', 'Title Chinese', 'required');

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ($this->form_validation->run() && $this->cate_create_sb())

        redirect('admin/link/cate', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/link/cate_review', 'refresh');

		}



	
		$data['main'] = 'link_cate_create';

    $data['title'] =$this->lang->line('title_link_cate_create');

		$this->load->view('admin/template_main', $data);
	}
		
	// function add category
	function cate_create_sb()
	{
		if(isset($_POST["create"]))
		{
		$name =array(
			"en" => $this->input->post("title_en"),
			"vi" => $this->input->post("title_vi"),
		//	"cn" => $this->input->post("title_cn")
			);
		$data = array(
			"link_cate_name" => serialize($name),
			
			);
	
	
			
		if($this->Mlink->insert_cate($data))
			return true;
		else
			return false;
		}
		else
			return false;
		
		
	}	
	// update product category
	
	function cate_update()
	{
	$id= $this->uri->segment(4);
	if ($this->input->post('update'))

    {
	

      //set rules


      $this->form_validation->set_rules('title_en', 'Title English', 'required');

      $this->form_validation->set_rules('title_vi', 'Title Vietnamese', 'required');

//     $this->form_validation->set_rules('title_cn', 'Title Chinese', 'required');

 //     $this->form_validation->set_rules('avatar', 'avatar', 'callback_upload_file');

      

      if ($this->form_validation->run() && $this->cate_update_proccess())

        redirect('admin/link/cate', 'refresh');

      else
		{
			
        	$data['missing'] = $this->form_validation->_error_array;
			
		}

    }

    elseif ($this->input->post('close'))

		{

			redirect('admin/link/cate', 'refresh');

		}



	
		$data['main'] = 'link_cate_update';
	$data['result']= $this->Mlink->info_from_id($id,'link_cate');
    $data['title'] = $this->lang->line('title_link_cate_update');

		$this->load->view('admin/template_main', $data);
	}
	
	// proccess data for update
	function cate_update_proccess()
	{
	$id= $this->uri->segment(4);
	if(isset($_POST["update"]))
		{
	$name =array(
			"en" => $this->input->post("title_en"),
			"vi" => $this->input->post("title_vi"),
	//		"cn" => $this->input->post("title_cn")
			);
		$data = array(
			"link_cate_name" => serialize($name),
			
			);
	
			
		if($this->Mlink->update_cate($data,$id))
			return true;
		else
			return false;
		}
		else
			return false;
	}
	
		

	
	
	
	function cate_request()
	{//determine 'action'
		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{
			if (in_array($key, $require))
			{
				$act = $key;
	
			}

		}
	//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mlink->cate_delete();

				} break;

			}//end switch



			redirect('admin/link/cate', 'refresh');

		}//end if

	}
	
		function request()
	{//determine 'action'
		$require = array('show', 'delete');

		foreach ($_POST as $key=>$value)

		{
			if (in_array($key, $require))
			{
				$act = $key;
	
			}

		}
	//process 'action'

		if (! empty($act))

		{

			switch ($act)

			{

				case 'show': //show

				{
				
					$_SESSION['row_number'] = $this->input->post('row_number');
					
				} break;



				case 'delete': //delete

				{

					$this->Mlink->delete();

				} break;

			}//end switch



			redirect('admin/link/', 'refresh');

		}//end if

	}

		public function delete($id)
	{
		if($id >0 )
		
			$this->Mlink->del_item($id);
		redirect('admin/link/', 'refresh');
		
	}	


}









?>
